package sw6.launcher;

import sw6.girafAppKit.GirafActivity;
import sw6.lib.Settings;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class launcher extends GirafActivity {
	protected final static String tag = "GIRAF"; 
	
	private final int Black = 1;
	private final int White = 2;
	private final int Red = 3;
	private final int Green = 4;
	private final int Blue = 5;
	private final int Yellow = 6;
	
	/** Called when the activity is first created. 
	 *  Populates the GridView and sets the onClickListner
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.launcher);
		
		GridView gridview = (GridView) findViewById(R.id.gridView);
	    gridview.setAdapter(IconAdapter.getInstance(this));

	    gridview.setOnItemClickListener(new OnItemClickListener() {
	        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
	        	
                String mInfo = (String)parent.getItemAtPosition(position);
                
                try {
                	Intent intent = getPackageManager().getLaunchIntentForPackage(mInfo);
                	intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                	startActivity(intent);
                } catch (Exception e) {
                	Toast.makeText(launcher.this, "The was an error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                }
	        }
	    });
		
	}
	
	@Override
	protected void onResume() {
		switch(Settings.getEnum(this, "bgcolor")) {
		case Black:
			findViewById(R.id.MainLayout).setBackgroundColor(Color.BLACK);
			break;
		case White:
			findViewById(R.id.MainLayout).setBackgroundColor(Color.WHITE);
			break;
		case Red:
			findViewById(R.id.MainLayout).setBackgroundColor(Color.RED);
			break;
		case Green:
			findViewById(R.id.MainLayout).setBackgroundColor(Color.GREEN);
			break;
		case Blue:
			findViewById(R.id.MainLayout).setBackgroundColor(Color.BLUE);
			break;
		case Yellow:
			findViewById(R.id.MainLayout).setBackgroundColor(Color.YELLOW);
			break;
			
		}
		
		((IconAdapter)((GridView) findViewById(R.id.gridView)).getAdapter()).updateApps();
		
		
		super.onResume();
	}

	//This comes from the home example, and disables the backkey for the launcher.
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (event.getKeyCode()) {
		case KeyEvent.KEYCODE_BACK:
			return true;
		case KeyEvent.KEYCODE_HOME:
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		switch (event.getKeyCode()) {
		case KeyEvent.KEYCODE_BACK:
			return true;
		case KeyEvent.KEYCODE_HOME:
			return true;
		}
		return super.onKeyUp(keyCode, event);
	}
	
	/** Called when the activity is destroyed, but does nothing special here */
	public void onDestroy() {
		super.onDestroy();
	}
}